using System.Windows.Forms;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Faces.Test.Win.Selectors;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMPropertyAndRuleEditorControl_AsBOColSelector : TestBOSelectorEditableGridControlWin
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        protected override IBusinessObject CreateNewBO()
        {
            return new DMProperty { PropertyName = TestUtilsShared.GetRandomString(10) };
        }
        protected static DMProperty CreateNewDMProperty(string boName)
        {
            return new DMProperty { PropertyName = boName };
        }
        protected override IBOColSelectorControl CreateSelector()
        {
            DMPropertyAndRuleEditorControl selectorControl = new DMPropertyAndRuleEditorControl(GetControlFactory());
            Form frm = new Form();
            frm.Controls.Add(selectorControl);
            return selectorControl;
        }
        protected override IBusinessObjectCollection GetCollectionWith_4_Objects()
        {
            DMProperty bo = CreateNewDMProperty("b");
            DMProperty bo2 = CreateNewDMProperty("d");
            DMProperty bo3 = CreateNewDMProperty("c");
            DMProperty bo4 = CreateNewDMProperty("a");
            return new BusinessObjectCollection<DMProperty> { bo, bo2, bo3, bo4 };
        }
        protected override IBusinessObjectCollection GetCollectionWithNoItems()
        {
            return new BusinessObjectCollection<DMProperty>();
        }

        protected override IBusinessObjectCollection GetCollectionWithOneBO(out IBusinessObject bo)
        {
            bo = CreateNewBO();
            return new BusinessObjectCollection<DMProperty> { (DMProperty)bo };
        }
        protected override IBusinessObjectCollection GetCollectionWithTowBOs(out IBusinessObject bo)
        {
            bo = CreateNewBO();
            DMProperty bo2 = (DMProperty)CreateNewBO();
            return new BusinessObjectCollection<DMProperty> { (DMProperty)bo, bo2 };
        }
        /// <summary>
        /// Need to solve getting the selectedIndex directly off the control necause this is causing
        /// me to expose GridControl all the time which means that the controls are not trully swappable.
        /// </summary>
        /// <param name="colSelector"></param>
        /// <param name="index"></param>
        protected override void SetSelectedIndex(IBOColSelectorControl colSelector, int index)
        {
            int count = 0;

            IGridBase gridSelector = ((DMPropertyAndRuleEditorControl)colSelector).GridControl.Grid;
            foreach (IDataGridViewRow row in gridSelector.Rows)
            {
                if (row == null) continue; //This is done to stop the Pragma warning.
                if (count == index)
                {
                    IBusinessObject businessObjectAtRow = gridSelector.GetBusinessObjectAtRow(count);
                    colSelector.SelectedBusinessObject = businessObjectAtRow;
                }
                count++;
            }
        }

        protected override int SelectedIndex(IBOColSelectorControl colSelector)
        {
            IGridBase gridSelector = ((DMPropertyAndRuleEditorControl)colSelector).GridControl.Grid;
            IDataGridViewRow currentRow = null;
            if (gridSelector.SelectedRows.Count > 0)
            {
                currentRow = gridSelector.SelectedRows[0];
            }

            if (currentRow == null) return -1;

            return gridSelector.Rows.IndexOf(currentRow);
        }
        [Test]
        public override void Test_Constructor_EditableGridControlSet()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBOColSelectorControl colSelector = CreateSelector();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMPropertyAndRuleEditorControl), colSelector);
        }


        [Test]
        public override void Test_Set_SelectedBusinessObject_ItemNotInList_SetsItemNull()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObject myBO;
            IBusinessObjectCollection collection = this.GetCollectionWithTowBOs(out myBO);
            IBusinessObject myBO2 = collection[1];
            colSelector.BusinessObjectCollection = collection;
            SetSelectedIndex(colSelector, ActualIndex(1));
            //---------------Assert Precondition----------------
            Assert.AreEqual(ActualNumberOfRows(collection.Count), colSelector.NoOfItems, "The blank item and others");
            Assert.AreEqual(ActualIndex(1), SelectedIndex(colSelector));
            Assert.AreEqual(myBO2, colSelector.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            colSelector.SelectedBusinessObject = CreateNewBO();
            //---------------Test Result -----------------------
            Assert.AreEqual(ActualNumberOfRows(2), colSelector.NoOfItems, "The blank item");
            Assert.IsNull(colSelector.SelectedBusinessObject);
            Assert.AreEqual(-1, SelectedIndex(colSelector));
        }

        [Test]
        public override void Test_SetBOCollection_WhenAutoSelectsFirstItem_ShouldSelectFirstItem()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObject myBO;
            IBusinessObjectCollection collection = this.GetCollectionWithTowBOs(out myBO);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, colSelector.NoOfItems);
            Assert.AreEqual(0, SelectedIndex(colSelector));
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(ActualNumberOfRows(collection.Count), colSelector.NoOfItems, "The blank item");
            Assert.AreSame(myBO, colSelector.SelectedBusinessObject);
            Assert.AreEqual(ActualIndex(0), SelectedIndex(colSelector));
        }


        [Test]
        public override void Test_AutoSelectsFirstItem_NoItems()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObjectCollection collection = GetCollectionWithNoItems();
            //---------------Assert Precondition----------------
            Assert.AreEqual(this.ActualNumberOfRows(0), colSelector.NoOfItems);
            Assert.AreEqual(0, SelectedIndex(colSelector));
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(ActualNumberOfRows(0), colSelector.NoOfItems, "The blank item");
            Assert.AreSame(null, colSelector.SelectedBusinessObject);
            Assert.AreEqual(0, SelectedIndex(colSelector));
        }
        [Test]
        public override void Test_SetBOCollection_WhenAutoSelectFalse_ShouldNot_AutoSelectsFirstItem()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObject myBO;
            IBusinessObjectCollection collection = GetCollectionWithTowBOs(out myBO);
            colSelector.AutoSelectFirstItem = false;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, colSelector.NoOfItems);
            Assert.AreEqual(0, SelectedIndex(colSelector));
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            Assert.IsFalse(colSelector.AutoSelectFirstItem);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(ActualNumberOfRows(collection.Count), colSelector.NoOfItems, "The blank item");
            Assert.IsNull(colSelector.SelectedBusinessObject);
        }
    }
}